package wang.aweb.childcar.controller.management;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.exception.CarException;
import wang.aweb.childcar.exception.CarExceptionEnum;
import wang.aweb.childcar.model.pojo.Device;
import wang.aweb.childcar.model.pojo.Fence;
import wang.aweb.childcar.model.pojo.WqPage;
import wang.aweb.childcar.model.req.FenceDeviceReq;
import wang.aweb.childcar.model.req.FenceReq;
import wang.aweb.childcar.service.DeviceService;
import wang.aweb.childcar.service.FenceService;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/management/fence/")
public class FenceController {
    @Autowired
    private FenceService fenceService;
    @Autowired
    private DeviceService deviceService;
    @GetMapping("getAll")
    public ApiRestResponse getAll(){
        List<Fence> fenceList=fenceService.getAll();
        return ApiRestResponse.success(fenceList);
    }
    //围栏列表
    @PostMapping("list")
    public ApiRestResponse list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        PageInfo pageInfo=fenceService.selectList(page,limit);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());
        return ApiRestResponse.success(wqPage);
    }
    @PostMapping("add")
    public ApiRestResponse add(@RequestBody FenceReq fenceReq){
        fenceService.add(fenceReq);
        return ApiRestResponse.success();
    }
    @PostMapping("getFence")
    public ApiRestResponse getFence(@RequestBody Map reqMap){
        if(StringUtils.isEmpty(reqMap.get("id"))){
            throw new CarException(CarExceptionEnum.ID_NULL);
        }
        Fence fence=fenceService.getInfo(Integer.valueOf((String)reqMap.get("id")));
        return ApiRestResponse.success(fence);
    }
    @PostMapping("update")
    public ApiRestResponse update(@RequestBody FenceReq fenceReq){
        fenceService.update(fenceReq);
        return ApiRestResponse.success();
    }
    @PostMapping("getDevice")
    public ApiRestResponse getDevice(@RequestBody Map reqMap){
        Integer id=Integer.valueOf((String)reqMap.get("id"));
        List<Device> deviceList=deviceService.getListByFenceId(id);
        return ApiRestResponse.success(deviceList);
    }
    @PostMapping("setDevice")
    public ApiRestResponse setDevice(@Valid @RequestBody FenceDeviceReq fenceDeviceReq){
        deviceService.setDeviceFenceIds(fenceDeviceReq);
        return ApiRestResponse.success();
    }
    @ApiOperation("删除围栏")
    @PostMapping("delete")
    public ApiRestResponse delete(@RequestBody Map<String,Integer> reqMap){
        List<Device> deviceList=deviceService.getListByFenceId(reqMap.get("id"));
        if(deviceList.size()>0){
            throw new CarException(CarExceptionEnum.DELETE_FAILED);
        }
        fenceService.delete(reqMap.get("id"));
        return ApiRestResponse.success();
    }
}
